﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ProperFraction
{
    public class FractionFourOperations
    {
        public int member { get; set; }
        public int denominator { get; set; }
        Random rd = new Random();
        public List<string> Value = new List<string>();

      
        string t = "";

        public void Reduction()
        {
            int gcd = GCD(member, denominator);
            member /= gcd;
            denominator /= gcd;
        }

        //先求最大公约数
        private int GCD(int a, int b)
        {
            int temp = 0;
            while (b != 0)
            {
                temp = a % b;
                a = b;
                b = temp;
            }
            return a;
        }
        public FractionFourOperations Mult(FractionFourOperations fra)
        {
            //存放加法结果
            FractionFourOperations sum = new FractionFourOperations();
            sum.member = member * fra.member;
            sum.denominator = denominator * fra.denominator;
            //约分
            sum.Reduction();

            return sum;
        }
        public List<string> runcheng(int count)
        {
            return grtcheng(count, out string value);
        }
      

        public List<string> grtcheng(int count, out string value)
        {
            FractionFourOperations fra_1 = new FractionFourOperations();
            FractionFourOperations fra_2 = new FractionFourOperations();
            value = null;
            List<string> vs = new List<string>();
            for (int i = 0; i < count; i++)
            {
                fra_1.member = rd.Next(1, 10);
                fra_1.denominator = rd.Next(1, 10);
                fra_1.Reduction();
                //定义另外一个数
                fra_2.member = rd.Next(1, 10);
                fra_2.denominator = rd.Next(1, 10);
                if (fra_1.member != fra_1.denominator && fra_2.member != fra_2.denominator)
                {
                    if(fra_1.member >fra_1.denominator&& fra_2.member > fra_2.denominator)
                    {
                        t = $"{fra_1.denominator}/{fra_1.member}*{fra_2.denominator}/{fra_2.member}=?";
                    }
                    else if(fra_1.member > fra_1.denominator&&fra_2.member < fra_2.denominator)
                    {
                        t = $"{fra_1.denominator}/{fra_1.member}*{fra_2.member}/{fra_2.denominator}=?";

                    }
                    else if(fra_1.member < fra_1.denominator && fra_2.member > fra_2.denominator)
                    {
                        t = $"{fra_1.member}/{fra_1.denominator}*{fra_2.denominator}/{fra_2.member}=?";
                    }
                    else
                    {
                        t = $"{fra_1.member}/{fra_1.denominator}*{fra_2.member}/{fra_2.denominator}=?";

                    }
                }             
                else if(fra_1.member == fra_1.denominator && fra_2.member <fra_2.denominator)
                {
                    t = $"{1}*{fra_2.member}/{fra_2.denominator}=?";
                }
                else if(fra_1.member == fra_1.denominator && fra_2.member > fra_2.denominator)
                {
                    t = $"{1}*{fra_2.denominator}/{fra_2.member}=?";
                }
                else if (fra_1.member < fra_1.denominator && fra_2.member == fra_2.denominator)
                {
                    t = $"{fra_1.member}/{fra_1.denominator}*{1}=?";
                }
                else
                {
                    t = $"{fra_1.denominator}/{fra_1.member}*{1}=?";
                }
                
                FractionFourOperations sum = fra_1.Mult(fra_2);
                vs.Add(t);
                Value.Add(sum.PrintFraction());
            }
            return vs;
        }
        public FractionFourOperations Sub(FractionFourOperations fra)
        {
            //存放加法结果
            FractionFourOperations sum = new FractionFourOperations();
            sum.member = member * fra.denominator - fra.member * denominator;
            sum.denominator = denominator * fra.denominator;
            //约分
            sum.Reduction();

            return sum;
        }
        public List<string> runjian(int count)
        {
            return getjian(count, out string value);
        }
        public List<string> getjian(int count, out string value)
        {
            FractionFourOperations fra_1 = new FractionFourOperations();
            FractionFourOperations fra_2 = new FractionFourOperations();
            value = null;
            List<string> vs = new List<string>();
            for (int i = 0; i < count; i++)
            {
                fra_1.member = rd.Next(1, 10);
                fra_1.denominator = rd.Next(1, 10);
                fra_1.Reduction();
                //定义另外一个数
                fra_2.member = rd.Next(1, 10);
                fra_2.denominator = rd.Next(1, 10);
                if (fra_1.member != fra_1.denominator && fra_2.member != fra_2.denominator)
                {
                    if(fra_1.member >fra_1.denominator&& fra_2.member > fra_2.denominator)
                    {
                        t = $"{fra_1.denominator}/{fra_1.member}-{fra_2.denominator}/{fra_2.member}=?";
                    }
                    else if(fra_1.member > fra_1.denominator&&fra_2.member < fra_2.denominator)
                    {
                        t = $"{fra_1.denominator}/{fra_1.member}-{fra_2.member}/{fra_2.denominator}=?";

                    }
                    else if(fra_1.member < fra_1.denominator && fra_2.member > fra_2.denominator)
                    {
                        t = $"{fra_1.member}/{fra_1.denominator}-{fra_2.denominator}/{fra_2.member}=?";
                    }
                    else
                    {
                        t = $"{fra_1.member}/{fra_1.denominator}-{fra_2.member}/{fra_2.denominator}=?";

                    }
                }             
                else if(fra_1.member == fra_1.denominator && fra_2.member <fra_2.denominator)
                {
                    t = $"{2}-{fra_2.member}/{fra_2.denominator}=?";
                }
                else if(fra_1.member == fra_1.denominator && fra_2.member > fra_2.denominator)
                {
                    t = $"{2}-{fra_2.denominator}/{fra_2.member}=?";
                }
                else if (fra_1.member < fra_1.denominator && fra_2.member == fra_2.denominator)
                {
                    t = $"{fra_1.member}/{fra_1.denominator}-{2}=?";
                }
                else
                {
                    t = $"{fra_1.denominator}/{fra_1.member}-{2}=?";
                }
                FractionFourOperations sum = fra_1.Sub(fra_2);
                vs.Add(t);
                Value.Add(sum.PrintFraction());
            }
            return vs;
        }
        //分子分母交换
        public FractionFourOperations Exchange()
        {
            FractionFourOperations NewFra = new FractionFourOperations();

            NewFra.denominator = this.member;
            NewFra.member = this.denominator;
            return NewFra;
        }

        public FractionFourOperations Division(FractionFourOperations fra)
        {
            //存放加法结果
            FractionFourOperations sum = new FractionFourOperations();
            //求倒数
            FractionFourOperations newfra = fra.Exchange();

            sum.member = this.member * newfra.member;
            sum.denominator = this.denominator * newfra.denominator;
            //约分
            sum.Reduction();

            return sum;
        }
        public List<string> runchu(int count)
        {
            return grtchu(count, out string value);
        }
        public List<string> grtchu(int count, out string value)
        {
            FractionFourOperations fra_1 = new FractionFourOperations();
            FractionFourOperations fra_2 = new FractionFourOperations();
            string t = "";
            value = null;
            List<string> vs = new List<string>();
            for (int i = 0; i < count; i++)
            {
                fra_1.member = rd.Next(1, 10);
                fra_1.denominator = rd.Next(1, 10);
                fra_1.Reduction();
                //定义另外一个数
                fra_2.member = rd.Next(1, 10);
                fra_2.denominator = rd.Next(1, 10);
                if (fra_1.member != fra_1.denominator && fra_2.member != fra_2.denominator)
                {
                    if (fra_1.member > fra_1.denominator && fra_2.member > fra_2.denominator)
                    {
                        t = $"{fra_1.denominator}/{fra_1.member} / {fra_2.denominator}/{fra_2.member}=?";
                    }
                    else if (fra_1.member > fra_1.denominator && fra_2.member < fra_2.denominator)
                    {
                        t = $"{fra_1.denominator}/{fra_1.member} / {fra_2.member}/{fra_2.denominator}=?";

                    }
                    else if (fra_1.member < fra_1.denominator && fra_2.member > fra_2.denominator)
                    {
                        t = $"{fra_1.member}/{fra_1.denominator} / {fra_2.denominator}/{fra_2.member}=?";
                    }
                    else
                    {
                        t = $"{fra_1.member}/{fra_1.denominator} / {fra_2.member}/{fra_2.denominator}=?";

                    }
                }
                else if (fra_1.member == fra_1.denominator && fra_2.member < fra_2.denominator)
                {
                    t = $"{1} / {fra_2.member}/{fra_2.denominator}=?";
                }
                else if (fra_1.member == fra_1.denominator && fra_2.member > fra_2.denominator)
                {
                    t = $"{1} / {fra_2.denominator}/{fra_2.member}=?";
                }
                else if (fra_1.member < fra_1.denominator && fra_2.member == fra_2.denominator)
                {
                    t = $"{fra_1.member}/{fra_1.denominator} / {1}=?";
                }
                else
                {
                    t = $"{fra_1.denominator}/{fra_1.member} / {1}=?";
                }
                FractionFourOperations sum = fra_1.Division(fra_2);
                vs.Add(t);
                Value.Add(sum.PrintFraction());
            }
            return vs;
        }
        public FractionFourOperations Add(FractionFourOperations fra)
        {
            //存放加法结果
            FractionFourOperations sum = new FractionFourOperations();
            sum.member = this.member * fra.denominator + fra.member * this.denominator;
            sum.denominator = this.denominator * fra.denominator;
            //约分
            sum.Reduction();

            return sum;
        }
        public List<string> runjia(int count)
        {
            return getjia(count, out string value);
        }
        public List<string> getjia(int count, out string value)
        {
            FractionFourOperations fra_1 = new FractionFourOperations();
            FractionFourOperations fra_2 = new FractionFourOperations();
            value = null;
            List<string> vs = new List<string>();
            for (int i = 0; i < count; i++)
            {
                fra_1.member = rd.Next(1, 10);
                fra_1.denominator = rd.Next(1, 10);
                fra_1.Reduction();
                //定义另外一个数
                fra_2.member = rd.Next(1, 10);
                fra_2.denominator = rd.Next(1, 10);
                if (fra_1.member != fra_1.denominator && fra_2.member != fra_2.denominator)
                {
                    if (fra_1.member > fra_1.denominator && fra_2.member > fra_2.denominator)
                    {
                        t = $"{fra_1.denominator}/{fra_1.member}+{fra_2.denominator}/{fra_2.member}=?";
                    }
                    else if (fra_1.member > fra_1.denominator && fra_2.member < fra_2.denominator)
                    {
                        t = $"{fra_1.denominator}/{fra_1.member}+{fra_2.member}/{fra_2.denominator}=?";

                    }
                    else if (fra_1.member < fra_1.denominator && fra_2.member > fra_2.denominator)
                    {
                        t = $"{fra_1.member}/{fra_1.denominator}+{fra_2.denominator}/{fra_2.member}=?";
                    }
                    else
                    {
                        t = $"{fra_1.member}/{fra_1.denominator}+{fra_2.member}/{fra_2.denominator}=?";

                    }
                }
                else if (fra_1.member == fra_1.denominator && fra_2.member < fra_2.denominator)
                {
                    t = $"{1}+{fra_2.member}/{fra_2.denominator}=?";
                }
                else if (fra_1.member == fra_1.denominator && fra_2.member > fra_2.denominator)
                {
                    t = $"{1}+{fra_2.denominator}/{fra_2.member}=?";
                }
                else if (fra_1.member < fra_1.denominator && fra_2.member == fra_2.denominator)
                {
                    t = $"{fra_1.member}/{fra_1.denominator}+{1}=?";
                }
                else
                {
                    t = $"{fra_1.denominator}/{fra_1.member}+{1}=?";
                }
                FractionFourOperations sum = fra_1.Add(fra_2);
                vs.Add(t);
                Value.Add(sum.PrintFraction());
            }
            return vs;
        }
        //打印分数信息
        public string PrintFraction()
        {
            if (this.denominator == 1)
                return member.ToString();
            else if (this.member == 1)
                return denominator.ToString();
            else if (denominator == member)
                return "1";
            else
                return member + "/" + denominator;
        }
    }
}
